package com.etc.dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {
	//数据库驱动//
	String driver="com.mysql.jdbc.Driver";
	//数据库连接字符串//
	String url="jdbc:mysql://localhost:3306/gaoyangluntan3";
	 String username = "root";
	 String password = "gaojintian";
	 //获得数据库连接
	 public Connection getConnection() {
		 Connection conn=null;
		 try {
		 Class.forName("com.mysql.jdbc.Driver");
			
			conn = DriverManager.getConnection(url,username,password);
	 }catch(Exception e) {
		 e.printStackTrace();
	 }
		 return conn;
		 }
	 
	/**释放资源*/
	 public void closeAll(ResultSet rs,PreparedStatement ps,Connection conn) {
		 try {
			 if(rs!=null) {
			 rs.close();
			 }
			 if(ps!=null)
			 {
				 ps.close();
			 }
			 if(conn!=null)
			 {
				 conn.close();
			 }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 /**增删改查*/
	 public int myExecuteUpdate(String sql,Object[]args) {
		 int result=-10;//定义操作结果
		 if(sql==null||args==null) {
			 return -2;
		 }
		 
		 Connection conn = this.getConnection();
		 PreparedStatement ps = null;
		 try {
			 ps = (PreparedStatement) conn.prepareStatement(sql);
			 for (int i = 0; i < args.length; i++) {
				 ps.setObject(i + 1, args[i]);
				
			 }
			 result=ps.executeUpdate();
	 }catch(Exception e) {
		 e.printStackTrace();
	 }finally {
		 this.closeAll(null, ps, conn);
	 }
		 return result;
	 }
	 
//查询方法 加载对象
     public <T> T loadResult(Class<T> clazz,String sql,Object... args ) {
    	 if(sql==null || clazz==null)
    	 {
    		 return null;
    	 }
    	 T resultObject = null;
		 Connection conn = null;
		 PreparedStatement ps = null;
		 ResultSet rs = null;
		 
		 try {
			conn = this.getConnection();
			 ps = conn.prepareStatement(sql);
			 //参数赋值
			 if(args !=null)
			 {   
				 for(int i=0;i<args.length;i++)
				 {
				 ps.setObject(i+1, args[i]);
				 }
			 }
			 rs = ps.executeQuery();
			 ResultSetMetaData metaData = rs.getMetaData();
			 int columLength = metaData.getColumnCount();
			 while(rs.next())
			 {
				 resultObject = clazz.newInstance();
				 for(int i=0;i<columLength;i++)
				 {
					 //数据封装
					 String colName = metaData.getColumnName(i+1);//获得字段名称
					 Object colValue = rs.getObject(colName);//获得对应字段的值
					 if(colValue==null)
					 {
						 colValue = "";
					 }
					 Field field = clazz.getDeclaredField(colName);
					 field.setAccessible(true);
					 field.set(resultObject,colValue);
					 
				 }
			 }
		 }
		 catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}finally
		 {
			this.closeAll(rs, ps, conn);
		 }
return resultObject;
}
     //查询方法2 查询多个对象，返回对象集合
     public <T> List<T> listResult(Class<T> clazz,String sql,Object... args)
     {
    	 if(sql ==null || clazz ==null)
    	 {
    		 return null;
    	 }
    	 List<T> list = new ArrayList<T>();
    	 Connection conn = null;
    	 PreparedStatement ps = null;
    	 ResultSet rs = null;
    	 try
    	 {
    		 conn = this.getConnection();
    		 ps = conn.prepareStatement(sql);
    		 if(args!=null)
    		 {
    			 for(int i=0;i<args.length;i++)
    			 {
    				 ps.setObject(i+1, args[i]);
    			 }
    		 }
    		 rs=ps.executeQuery();
    		 ResultSetMetaData metaData = rs.getMetaData();
    		 int columLength = metaData.getColumnCount();
    		 while(rs.next())
    		 {
    			 T resultObject = clazz.newInstance();
    			 for(int i=0;i<columLength;i++)
    			 {
    				 String colName = metaData.getColumnName(i+1);
    				 Object colValue = rs.getObject(colName);
    				 if(colValue == null)
    				 {
    					 colValue = "";
    				 }
    				 Field field = clazz.getDeclaredField(colName);
    				 field.setAccessible(true);
    				 field.set(resultObject, colValue);
    			 }
    			 list.add(resultObject);
    		 }
    	 }
    	 catch(Exception e)
    	 {
    		 e.printStackTrace();
    	 }
    	 finally
    	 {
    		 this.closeAll(rs, ps, conn);
    	 }
    	 return list;
     }
}
